/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 11.05.2009, 08:38:17
 */
package dialogs;

import BusinessLayer.BusinessLayer;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.KeyEvent;
import logging.Logger;
import wisi.WiSiView;

/**
 *
 * @author Benji
 */
public class Login extends javax.swing.JDialog {

    Frame parent;
    /** Creates new form Login */
    public Login(java.awt.Frame parent, boolean modal) {

        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(okButton);

        Logger.debug("Öffne " + this.getName());

        this.parent = parent;
        int x;
        int y;
        Point topLeft = parent.getLocationOnScreen();
        Dimension sizeOfParent = parent.getSize();
        Dimension mySize = getSize();

        if (sizeOfParent.width > mySize.width) {
            x = ((sizeOfParent.width - mySize.width) / 2) + topLeft.x;
        } else {
            x = topLeft.x;
        }

        if (sizeOfParent.height > mySize.height) {
            y = ((sizeOfParent.height - mySize.height) / 2) + topLeft.y;
        } else {
            y = topLeft.y;
        }

        setLocation(x, y);


    }

    private void doLogin() {
        String user = null;
        String password = null;

        //sind alle Felder befüllt
        if (this.nameTextField.getText().isEmpty()) {
            this.errorLabel.setText("Bitte Username eingeben!");
            Logger.trace("kein name eigegeben");
        } else if (new String(this.PasswordTextField.getPassword()).isEmpty()) {
            this.errorLabel.setText("Bitte Passwort eingeben!");
            Logger.trace("kein Passwort eingegeben");
        } else {
            this.errorLabel.setText(" ");
            user = this.nameTextField.getText();
            password = new String(this.PasswordTextField.getPassword());
            if (!BusinessLayer.checkUser(user)) {
                this.errorLabel.setText("Username und Passwort passen nicht zusammen");
                Logger.warn("Name oder Passwort falsch");
            } else if (!BusinessLayer.checkPassword(user, password)) {
                this.errorLabel.setText("Username und Passwort passen nicht zusammen");
                Logger.warn("Name oder Passwort falsch");
            } else if (BusinessLayer.checkPassword(user, password)) {

                //daten des Users speichern
                BusinessLayer.setUserId(WiSiView.business.getUserId(user, password));
                BusinessLayer.setLoggedIn(true);
                Logger.trace("Login erfolgreich");
                this.dispose();
                Logger.debug("Schliese Dialog" + this.getName());
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        PasswordTextField = new javax.swing.JPasswordField();
        abbruchButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();
        registerButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(new java.awt.Rectangle(500, 500, 0, 0));
        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wisi.WiSiApp.class).getContext().getResourceMap(Login.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N
        nameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameTextFieldKeyReleased(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        PasswordTextField.setText(resourceMap.getString("PasswordTextField.text")); // NOI18N
        PasswordTextField.setName("PasswordTextField"); // NOI18N
        PasswordTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                PasswordTextFieldKeyReleased(evt);
            }
        });

        abbruchButton.setText(resourceMap.getString("abbruchButton.text")); // NOI18N
        abbruchButton.setName("abbruchButton"); // NOI18N
        abbruchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abbruchButtonActionPerformed(evt);
            }
        });

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        errorLabel.setForeground(resourceMap.getColor("errorLabel.foreground")); // NOI18N
        errorLabel.setText(resourceMap.getString("errorLabel.text")); // NOI18N
        errorLabel.setName("errorLabel"); // NOI18N

        registerButton.setText(resourceMap.getString("registerButton.text")); // NOI18N
        registerButton.setName("registerButton"); // NOI18N
        registerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                            .addComponent(PasswordTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(registerButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                        .addComponent(abbruchButton))
                    .addComponent(errorLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(PasswordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(errorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(registerButton)
                    .addComponent(abbruchButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

        doLogin();



    }//GEN-LAST:event_okButtonActionPerformed

    private void abbruchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abbruchButtonActionPerformed
        this.dispose();
        Logger.debug("Schliese Dialog" + this.getName());
    }//GEN-LAST:event_abbruchButtonActionPerformed

    private void PasswordTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PasswordTextFieldKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            doLogin();
        }
    }//GEN-LAST:event_PasswordTextFieldKeyReleased

    private void nameTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTextFieldKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (new String(this.PasswordTextField.getPassword()).isEmpty()) {
                this.PasswordTextField.requestFocusInWindow();
            } else {
                doLogin();
            }
        }
    }//GEN-LAST:event_nameTextFieldKeyReleased

    private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerButtonActionPerformed
        new Register(this.parent, true).setVisible(true);
}//GEN-LAST:event_registerButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Login dialog = new Login(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField PasswordTextField;
    private javax.swing.JButton abbruchButton;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JButton registerButton;
    // End of variables declaration//GEN-END:variables
}
